package com.example.consumerone.controller;

import com.example.common.base.Paging;
import com.example.common.base.Result;
import com.example.common.model.ActivityArticle;
import com.example.common.model.User;
import com.example.consumerone.services.ActivityArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/activityArticle")
@CrossOrigin(origins = "${com.smart.origins}")
public class ActivityArticleController {

    @Autowired
    private ActivityArticleService activityArticleService;

    @RequestMapping("/test")
    public String test(){
        return activityArticleService.test();
    }

   // @PostMapping("/test2")
//    public User test2(@RequestBody User user){
//        return user;
//    }
    //根据类型获取activity列表
    @RequestMapping(value = "/allActivityTypeList/{type}", method = RequestMethod.GET)
    public Map<String, Object> getActivityArticleList(@PathVariable("type") Integer type){
        return activityArticleService.getActivityArticleList(type);
    }

    /**
     *  分页查询list
     */
    @PostMapping("/list")
    public Paging<ActivityArticle> list(@RequestParam("pageNum") Integer pageNum,
                                        @RequestParam("pageSize") Integer pageSize,
                                        @RequestParam("type") Integer activityType) {
        return  activityArticleService.list(pageNum, pageSize, activityType);
    }


    /**
     *  根据aId查询文章
     */
    @GetMapping("/get")
    public Map<String, Object> getDetail(
            @RequestParam("aId") Integer aId,
            @RequestParam("userId") String userId,
            @RequestParam("authorId") String authorId){
        Map<String, Object> detail = activityArticleService.getDetail(aId, userId, authorId);
        System.out.println(detail);
        return detail;
    }

    /**
     *  参与活动
     */
    @PostMapping("/addArticleArticleParticipants")
    public Result<?> addArticleArticleParticipants(
            @RequestParam("aId") Integer aId,
            @RequestParam("userId") String userId,
            @RequestParam("userName") String userName,
            @RequestParam("userAvatarUrl")String userAvatarUrl){
        Boolean aBoolean = activityArticleService.addArticleArticleParticipants(aId, userId, userName, userAvatarUrl);
        if (aBoolean){
            return Result.ok("活动参与成功");
        }
        return Result.error("参与失败");
    }

    /**
     *  取消参与活动
     */
    @PostMapping("/reArticleArticleParticipants")
    public Result<?> reArticleArticleParticipants(
            @RequestParam("aId") Integer aId,
            @RequestParam("userId") String userId){
        Boolean aBoolean = activityArticleService.reArticleArticleParticipants(aId, userId);
        if (aBoolean){
            return Result.ok("活动参与成功");
        }
        return Result.error("活动参与失败");
    }
}
